home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / Tools & Goodies / IntlTest / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-04-23  |  13.4 KB  |  426 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Content.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "IntlTest.hpp"
  11.  
  12. #ifndef CONTENT_H
  13. #include "Content.h"
  14. #endif
  15.  
  16. #ifndef PART_H
  17. #include "Part.h"
  18. #endif
  19.  
  20. // --- Framework Includes ---
  21.  
  22. #ifndef FWEDVIEW_H
  23. #include "FWEdView.h"
  24. #endif
  25.  
  26. #ifndef FWUTIL_H
  27. #include "FWUtil.h"
  28. #endif
  29.  
  30. // --- OpenDoc Includes ---
  31.  
  32. // for ODSetITextProp
  33. #ifndef _STDTYPIO_
  34. #include "StdTypIO.h"
  35. #endif
  36.  
  37. // ----- Macintosh Includes -----
  38.  
  39. #ifndef __SCRIPT__
  40. #include <Script.h>
  41. #endif
  42.  
  43. //========================================================================================
  44. //    Runtime info
  45. //========================================================================================
  46.  
  47. #ifdef FW_BUILD_MAC
  48. #pragma segment odfIntlTest
  49. #endif
  50.  
  51. //========================================================================================
  52. //    Global Functions
  53. //========================================================================================
  54.  
  55. FW_Locale gJapaneseLocale = { smJapanese, langJapanese };
  56.  
  57. //----------------------------------------------------------------------------------------
  58. //    InternalizeIntlText
  59. //----------------------------------------------------------------------------------------
  60. FW_Boolean InternalizeIntlText(Environment* ev, ODStorageUnit* storageUnit, FW_CString& textData)
  61. {
  62.     FW_Boolean result = false;
  63.     ODIText* iText = ODGetITextProp(ev, storageUnit, kODPropContents, kODIntlText, NULL);
  64.     if (iText)
  65.     {
  66.         textData.ReplaceAll(iText);
  67.         /* Do we need to free the iText? */
  68.         result = true;
  69.     }
  70.  
  71.     return result;
  72. }
  73.  
  74. //----------------------------------------------------------------------------------------
  75. //    InternalizeText
  76. //----------------------------------------------------------------------------------------
  77. FW_Boolean InternalizeText(Environment* ev, ODStorageUnit* storageUnit, FW_CString& textData)
  78. {
  79.     FW_Boolean result = false;
  80.  
  81. #ifdef FW_BUILD_MAC
  82.     unsigned long textSize = storageUnit->GetSize(ev);
  83.     if (textSize > 0)
  84.     {
  85.         char buffer[256];
  86.         if (textSize > 255) 
  87.             textSize = 255;
  88.         FW_CByteArray byteArray;
  89.         storageUnit->GetValue(ev, textSize, byteArray);
  90.         byteArray.CopyBuffer(&buffer, textSize);
  91.         
  92.         textData = "";
  93.         textData.Append((const FW_Char*) &buffer, textSize);
  94.         
  95.         result = TRUE;
  96.     }
  97. #endif
  98.  
  99.     return result;
  100. }
  101.  
  102. //========================================================================================
  103. //    CIntlTestContent class
  104. //========================================================================================
  105.  
  106. FW_DEFINE_AUTO(CIntlTestContent)
  107.  
  108. //----------------------------------------------------------------------------------------
  109. // CIntlTestContent constructor
  110. //----------------------------------------------------------------------------------------
  111. CIntlTestContent::CIntlTestContent(Environment* ev, CIntlTestPart* part) :
  112.     FW_CContent(ev, part),
  113.     fEnglishText(""),
  114.     fJapaneseText("", gJapaneseLocale),
  115.     fTextData(""),
  116.     fIntlTestPart(part)
  117. {
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. // CIntlTestContent destructor
  122. //----------------------------------------------------------------------------------------
  123.  
  124. CIntlTestContent::~CIntlTestContent()
  125. {
  126. }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // CIntlTestContent::GetTextData
  130. //----------------------------------------------------------------------------------------
  131.  
  132. const FW_CString& CIntlTestContent::GetTextData(ODID whichView)
  133. {
  134.     switch (whichView)
  135.     {
  136.         case kEnglishEditView:
  137.             return fEnglishText;
  138.         case kJapaneseEditView:
  139.             return fJapaneseText;
  140.     }
  141.  
  142.     return fTextData;
  143. }
  144.  
  145. //----------------------------------------------------------------------------------------
  146. // CIntlTestContent::SetTextData
  147. //----------------------------------------------------------------------------------------
  148.  
  149. void CIntlTestContent::SetTextData(    Environment* ev, 
  150.                                     const FW_CString& newText, 
  151.                                     ODID whichView)
  152. {
  153.     switch (whichView)
  154.     {
  155.         case kEnglishEditView:
  156.             fEnglishText = newText;
  157.             break;
  158.         case kJapaneseEditView:
  159.             fJapaneseText = newText;
  160.             break;
  161.         default:
  162.             fTextData = newText;
  163.     }
  164.  
  165.     fIntlTestPart->PartChanged(ev);
  166. }
  167.  
  168. //----------------------------------------------------------------------------------------
  169. // CIntlTestContent::Externalize
  170. //----------------------------------------------------------------------------------------
  171.  
  172. void CIntlTestContent::Externalize(    Environment* ev,
  173.                                     ODStorageUnit* storageUnit,
  174.                                     FW_EStorageKinds storageKind,
  175.                                     FW_CCloneInfo* cloneInfo)
  176. {
  177.     FW_UNUSED(cloneInfo);
  178.     FW_UNUSED(storageKind);
  179.  
  180.     // Update the strings from the edit views, if we have any
  181.     fIntlTestPart->UpdateTextData(ev);
  182.  
  183.     // Externalize in order of fidelity: part kind, ODIText, 'TEXT'
  184.     ExternalizePartKind(ev, storageUnit);
  185.     ExternalizeIntlText(ev, storageUnit);
  186.     ExternalizeText(ev, storageUnit);
  187. }
  188.  
  189. //----------------------------------------------------------------------------------------
  190. // CIntlTestContent::ExternalizePartKind
  191. //----------------------------------------------------------------------------------------
  192.  
  193. void CIntlTestContent::ExternalizePartKind(Environment* ev, ODStorageUnit* storageUnit)
  194. {
  195.     // Write the three text data strings to storage
  196.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fIntlTestPart->GetPartKind(ev));
  197.     FW_CWritableStream archive(suSink);
  198.  
  199.     unsigned long stringCount = 3;
  200.     archive << stringCount;
  201.  
  202.     unsigned long whichString = kTextEditView;
  203.     archive << whichString;
  204.     archive << fTextData;
  205.  
  206.     whichString = kEnglishEditView;
  207.     archive << whichString;
  208.     archive << fEnglishText;
  209.  
  210.     whichString = kJapaneseEditView;
  211.     archive << whichString;
  212.     archive << fJapaneseText;
  213. }
  214.  
  215. //----------------------------------------------------------------------------------------
  216. // CIntlTestContent::ExternalizeIntlText
  217. //----------------------------------------------------------------------------------------
  218. void CIntlTestContent::ExternalizeIntlText(Environment* ev, ODStorageUnit* destinationSU)
  219. {
  220.     // Write the main string as an ODIText
  221.     ODSetITextProp(ev, destinationSU, kODPropContents, kODIntlText, fTextData.RevealODIText());
  222. }
  223.  
  224. //----------------------------------------------------------------------------------------
  225. // CIntlTestContent::ExternalizeText
  226. //----------------------------------------------------------------------------------------
  227. void CIntlTestContent::ExternalizeText(Environment* ev, ODStorageUnit* destinationSU)
  228. {
  229. #ifdef FW_BUILD_MAC
  230.     // Write the main string in Mac 'TEXT' format
  231.     FW_SUAddPropValue(ev, destinationSU, kODPropContents, FW_CPart::gMacTEXTDataType);
  232.     FW_PStorageUnitSink suSink(ev, destinationSU, kODPropContents, FW_CPart::gMacTEXTDataType);
  233.     FW_CWritableStream stream(suSink);
  234.     stream.Write(fTextData.RevealBuffer(), fTextData.GetByteLength());
  235. #endif
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. // CIntlTestContent::Internalize
  240. //----------------------------------------------------------------------------------------
  241.  
  242. FW_Boolean CIntlTestContent::Internalize(Environment* ev,
  243.                                          ODStorageUnit* sourceSU, 
  244.                                          FW_EStorageKinds storageKind,
  245.                                          FW_CCloneInfo* cloneInfo)
  246. {
  247.     FW_UNUSED(cloneInfo);
  248.     FW_Boolean internalized = FALSE;
  249.     
  250.     if (FW_SUExistsThenFocus(ev, sourceSU, kODPropContents, fIntlTestPart->GetPartKind(ev)))
  251.         internalized = this->InternalizePartKind(ev, sourceSU);
  252. /* See note in CIntlTestSelectedContent::Externalize
  253.     else if (FW_SUExistsThenFocus(ev, sourceSU, kODPropContents, kODIntlText))    // ODIText
  254.         internalized = InternalizeIntlText(ev, sourceSU, fTextData);
  255. */
  256.     else if (FW_SUExistsThenFocus(ev, sourceSU, kODPropContents, FW_CPart::gMacTEXTDataType))    // 'TEXT'
  257.         internalized = InternalizeText(ev, sourceSU, fTextData);
  258.  
  259.     if (internalized && storageKind != FW_kPartStorage)
  260.     {
  261.         fIntlTestPart->PartChanged(ev);
  262.     }
  263.         
  264.     return internalized;
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. //    CIntlTestContent::InternalizePartKind
  269. //----------------------------------------------------------------------------------------
  270.  
  271. FW_Boolean CIntlTestContent::InternalizePartKind(Environment* ev, ODStorageUnit* storageUnit)
  272. {
  273.     FW_Boolean internalized = TRUE;
  274.  
  275.     if (storageUnit->GetSize(ev) != 0)
  276.     {
  277.         // Read the text data strings from storage
  278.         FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fIntlTestPart->GetPartKind(ev));
  279.         FW_PBufferedSink sink(ev, suSink);
  280.         FW_CReadableStream archive(sink);
  281.  
  282.         unsigned long stringCount, whichString;
  283.         archive >> stringCount;
  284.         for (unsigned long i = 0; i < stringCount; i++)
  285.         {
  286.             archive >> whichString;
  287.             switch (whichString)
  288.             {
  289.                 case kJapaneseEditView:
  290.                     archive >> fJapaneseText;
  291.                     break;
  292.                 case kEnglishEditView:
  293.                     archive >> fEnglishText;
  294.                     break;
  295.                 default:
  296.                     archive >> fTextData;
  297.             }
  298.         }
  299.     }
  300.     
  301.     return internalized;
  302. }
  303.  
  304. //----------------------------------------------------------------------------------------
  305. ODShape* CIntlTestContent::CreateDataFrameShape(Environment* ev) const
  306. {
  307.     FW_CRect defaultRect(FW_kFixed0, FW_kFixed0, FW_IntToFixed(320), FW_IntToFixed(250));
  308.     ODShape* defaultShape = ::FW_NewODShape(ev, defaultRect);
  309.     return defaultShape;
  310. }
  311.  
  312. //========================================================================================
  313. //    CIntlTestSelectedContent class
  314. //========================================================================================
  315.  
  316. FW_DEFINE_AUTO(CIntlTestSelectedContent)
  317.  
  318. //----------------------------------------------------------------------------------------
  319. // CIntlTestSelectedContent constructor
  320. //----------------------------------------------------------------------------------------
  321. CIntlTestSelectedContent::CIntlTestSelectedContent(Environment* ev, CIntlTestPart* part) :
  322.     FW_CContent(ev, part),
  323.     fIntlTestPart(part),
  324.     fEditView(NULL)
  325. {
  326. }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. // CIntlTestSelectedContent destructor
  330. //----------------------------------------------------------------------------------------
  331.  
  332. CIntlTestSelectedContent::~CIntlTestSelectedContent()
  333. {
  334. }
  335.  
  336. //----------------------------------------------------------------------------------------
  337. // CIntlTestSelectedContent::Externalize
  338. //----------------------------------------------------------------------------------------
  339. void CIntlTestSelectedContent::Externalize(Environment* ev,
  340.                                     ODStorageUnit* storageUnit,
  341.                                     FW_EStorageKinds storageKind,
  342.                                     FW_CCloneInfo* cloneInfo)
  343. {
  344.     FW_UNUSED(cloneInfo);
  345.     FW_UNUSED(storageKind);
  346.  
  347.     // Externalize in order of fidelity: ODIText, 'TEXT'
  348.     FW_CString selectedText;
  349.     GetSelectedText(ev, selectedText);
  350.  
  351. /* This doesn't work, because when I Internalize the text it's in the form:
  352.     length word, char, etc. 
  353.     where each character takes up a word instead of a byte.
  354.     // Write the selected string as an ODIText
  355.     ODSetITextProp(ev, storageUnit, kODPropContents, kODIntlText, selectedText.RevealODIText());
  356. */
  357.  
  358.     // Write the selected string in Mac 'TEXT' format
  359.     FW_SUAddPropValue(ev, storageUnit, kODPropContents, FW_CPart::gMacTEXTDataType);
  360.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, FW_CPart::gMacTEXTDataType);
  361.     FW_CWritableStream stream(suSink);
  362.     stream.Write(selectedText.RevealBuffer(), selectedText.GetByteLength());
  363. }
  364.  
  365. //----------------------------------------------------------------------------------------
  366. // CIntlTestSelectedContent::Internalize
  367. //----------------------------------------------------------------------------------------
  368.  
  369. FW_Boolean CIntlTestSelectedContent::Internalize(Environment* ev,
  370.                                          ODStorageUnit* sourceSU, 
  371.                                          FW_EStorageKinds storageKind,
  372.                                          FW_CCloneInfo* cloneInfo)
  373. {
  374.     FW_UNUSED(cloneInfo);
  375.     FW_Boolean internalized = false;
  376.     FW_CString selectedText;
  377.  
  378. /* See note in Externalize
  379.     if (FW_SUExistsThenFocus(ev, sourceSU, kODPropContents, kODIntlText))    // ODIText
  380.         internalized = InternalizeIntlText(ev, sourceSU, selectedText);
  381.     else 
  382. */
  383.     if (FW_SUExistsThenFocus(ev, sourceSU, kODPropContents, FW_CPart::gMacTEXTDataType))    // 'TEXT'
  384.         internalized = InternalizeText(ev, sourceSU, selectedText);
  385.  
  386.     if (internalized)
  387.     {
  388.         fInternalizedText = selectedText;
  389.         this->SetSelectedText(ev, selectedText);
  390.         if (storageKind != FW_kPartStorage)
  391.             fIntlTestPart->PartChanged(ev);
  392.     }
  393.  
  394.     return internalized;
  395. }
  396.  
  397. //----------------------------------------------------------------------------------------
  398. void CIntlTestSelectedContent::GetSelectedText(Environment* ev, FW_CString& text)
  399. {
  400.     if (fEditView == NULL)
  401.         text = "";
  402.     else
  403.         text = fEditView->GetSelectedText(ev);
  404. }
  405.  
  406. //----------------------------------------------------------------------------------------
  407. void CIntlTestSelectedContent::SetSelectedText(Environment* ev, const FW_CString& newText)
  408. {
  409.     FW_ASSERT(fEditView);
  410.     fEditView->SetSelectedText(ev, newText);
  411. }
  412.  
  413. //----------------------------------------------------------------------------------------
  414. void CIntlTestSelectedContent::ClearSelectedText(Environment* ev)
  415. {
  416.     FW_ASSERT(fEditView);
  417.     fEditView->SetSelectedText(ev, FW_CString());
  418. }
  419.  
  420. //----------------------------------------------------------------------------------------
  421. void CIntlTestSelectedContent::SetSelectionRange(Environment* ev, short startOffset, short endOffset)
  422. {
  423.     if (fEditView)
  424.         fEditView->SelectText(ev, startOffset, endOffset);
  425. }
  426.